﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media.Media3D;

namespace Wen.UI.NavigationElements
{
	internal class SpotLightViewModel : IndicatorLightViewModelBase<SpotLight>
	{
		private readonly LightIndicator _positionIndicator;
		private readonly LightIndicator _directionIndicator;

		private readonly SpotLightPositionViewModel _spotLightPositionViewModel;
		public SpotLightPositionViewModel PositionViewModel {
			get { return _spotLightPositionViewModel; }
		}
		private readonly SpotLightDirectionViewModel _spotLightDirectionViewModel;
		public SpotLightDirectionViewModel DirectionViewModel {
			get { return _spotLightDirectionViewModel; }
		}

		public SpotLightViewModel(SpotLight light, Viewport3D viewport3D)
			: base(light, viewport3D) {
			_spotLightDirectionViewModel = new SpotLightDirectionViewModel(light);
			_spotLightPositionViewModel = new SpotLightPositionViewModel(light);
			_spotLightDirectionViewModel.ValueChanged += (s, e) => RefreshIndicatorsPosition();
			_spotLightPositionViewModel.ValueChanged += (s, e) => RefreshIndicatorsPosition();
			_directionIndicator = new LightIndicator(viewport3D, true, DirectionIndicatorPosition);
			_positionIndicator = new LightIndicator(viewport3D, false, light.Position);
		}

		private Point3D DirectionIndicatorPosition {
			get {
				return new Point3D(Light.Position.X + Light.Direction.X, Light.Position.Y + Light.Direction.Y, Light.Position.Z + Light.Direction.Z);
			}
		}

		protected override IEnumerable<LightIndicator> Indicators {
			get {
				yield return _positionIndicator;
				yield return _directionIndicator;
			}
		}

		protected override void RefreshIndicatorsPosition() {
			_positionIndicator.Position = Light.Position;
			_directionIndicator.Position = DirectionIndicatorPosition;
		}
	}
}
